package com.hs.telepass.ui;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.hs.teleform.R;
import com.hs.telepass.manager.PreferencesManager;
import com.hs.telepass.utils.ImageUtil;

public class MainActivity extends FragmentActivity {

    public static final int TAB_INDEX_COUNT = 3;
    public static final int TAB_HISTORY_ITEM = 0;
    public static final int TAB_CAMERA_ITEM = 1;
    public static final int TAB_SETTING_ITEM = 2;
    @InjectView(R.id.bottom_btn_history)
    ImageView mBottomBtnHistory;
    @InjectView(R.id.bottom_btn_capture)
    ImageView mBottomBtnCapture;
    @InjectView(R.id.bottom_btn_setting)
    ImageView mBottomBtnSetting;

    ImageView mLastBtn;
    int mLastDrawable;

    private int mInitTabIndex;

    private ViewPager mPager;
    private ViewPagerAdapter mViewPagerAdapter;

    private HistoryFragment mHistoryFragment = new HistoryFragment();
    private CaptureFragment mCaptureFragment = new CaptureFragment();
    private SettingFragment mSettingFragment = new SettingFragment();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activiy);
        ButterKnife.inject(this);

        mInitTabIndex = getIntent().getIntExtra("initTabIndex", 1);

        findViews();
        initViewPager();

        zoomBtnHistory(60);
        zoomBtnSetting(60);
        zoomBtnCapture(80);

        PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, "9ejBYhSnKEeNMXAWtUKB5MRm");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(true == PreferencesManager.getInstance().getNotificationSwitch())
            showNotification();
    }

    @OnClick(R.id.bottom_btn_history)
    void onHistoryClick(){
        mPager.setCurrentItem(TAB_HISTORY_ITEM);
    }

    @OnClick(R.id.bottom_btn_capture)
    void onCaptureClick(){
        mPager.setCurrentItem(TAB_CAMERA_ITEM);
    }

    @OnClick(R.id.bottom_btn_setting)
    void onSettingClick(){
        mPager.setCurrentItem(TAB_SETTING_ITEM);
    }

    private void findViews() {
        mPager = (ViewPager) findViewById(R.id.vPager);
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        mPager.setAdapter(mViewPagerAdapter);
        mPager.setOffscreenPageLimit(3);
        mPager.setCurrentItem(mInitTabIndex);
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                zoomImageBtn(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    /**
     * 放大底部中间的button*/
    private void zoomBtnCapture(int size){
        mLastBtn = mBottomBtnCapture;
        mLastDrawable = R.drawable.btn_key;
        Bitmap bigBitmap = ImageUtil.zoomBitmap(mLastDrawable, size, size);
        mLastBtn.setImageBitmap(bigBitmap);
    }

    private void zoomBtnHistory(int size){
        Bitmap bigProifleBitmap = ImageUtil.zoomBitmap(R.drawable.btn_profile, size, size);
        mBottomBtnHistory.setImageBitmap(bigProifleBitmap);
        mLastBtn = mBottomBtnHistory;
        mLastDrawable = R.drawable.btn_profile;
    }

    private void zoomBtnSetting(int size){
        Bitmap bigSettingBitmap = ImageUtil.zoomBitmap(R.drawable.btn_settings, size, size);
        mBottomBtnSetting.setImageBitmap(bigSettingBitmap);
        mLastBtn = mBottomBtnSetting;
        mLastDrawable = R.drawable.btn_settings;
    }


    private void zoomImageBtn(int tab_item){

        Bitmap revertBitmap = ImageUtil.zoomBitmap(mLastDrawable, 60, 60);
        mLastBtn.setImageBitmap(revertBitmap);

        switch(tab_item){
            case TAB_HISTORY_ITEM:
                zoomBtnHistory(80);
                break;
            case TAB_CAMERA_ITEM:
                zoomBtnCapture(80);
                break;
            case TAB_SETTING_ITEM:
                zoomBtnSetting(80);
                break;
        }
    }

    public int getCurrentItem(){
        return mPager.getCurrentItem();
    }


    public class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return mHistoryFragment;
                case 1:
                    return mCaptureFragment;
                case 2:
                    return mSettingFragment;

            }
            throw new IllegalStateException("No fragment at position "
                    + position);
        }

        @Override
        public int getCount() {
            return TAB_INDEX_COUNT;
        }


    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void showNotification(){
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(
                this,
                R.string.app_name,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification =new Notification.Builder(this)
                                        .setContentTitle("Singou")
                                        .setContentText(getString(R.string.notification_tip))
                                        .setLargeIcon(ImageUtil.zoomBitmap(R.drawable.btn_key, 100, 100))
                                        .setSmallIcon(R.drawable.icon)
                                        .setContentIntent(contentIntent)
                                        .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        nm.notify(R.string.app_name, notification);
    }
}